Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module use genericity can not set null #918

Closed
supemeko opened this issue Jan 4, 2020 · 2 comments
Closed

module use genericity can not set null #918

supemeko opened this issue Jan 4, 2020 · 2 comments

Comments

@supemeko
Copy link

supemeko commented Jan 4, 2020

show a error:

public module Storage<T>
    T t
    function setNull()
        t = null // error: Cannot assign null to int

public class StorageInt
    use Storage<int>

next is correct:

public module Storage<T>
    T t
    function setNull()
        t = null

class Int
    int value

public class StorageInt
    use Storage<Int>
@supemeko supemeko changed the title genericity can not set null module use genericity can not set null Jan 4, 2020
@peq
Copy link
Collaborator

peq commented Jan 4, 2020

This is intended behavior. It is the same as when you write int x = null. The literal null is only valid for object and handle types.

If you need a default value in a module, I suggest that you add an abstract function default() returns T or add another boolean attribute to keep track whether the element is defined or not.

With the planned new generics (#679) it will also be possible to use a type class for this.

@peq peq closed this as completed Jan 4, 2020
@supemeko
Copy link
Author

supemeko commented Jan 5, 2020

This is intended behavior. It is the same as when you write int x = null. The literal null is only valid for object and handle types.

If you need a default value in a module, I suggest that you add an abstract function default() returns T or add another boolean attribute to keep track whether the element is defined or not.

With the planned new generics (#679) it will also be possible to use a type class for this.

but, It is correct in the a class,

public class Storage<T>
    T t
    construct()
        t = null
    function set(T t)
        this.t = t

enum _enum
    A
    B

function _enumFromIndex(int i) returns _Tuple
    return _Tuple(i.toString())

function _enumToIndex(_enum i) returns int
    return i castTo int

class Class
    int num
    construct(int num)
        this.num = num

tuple _Tuple(string num)

function _TupleFromIndex(int i) returns _Tuple
    return _Tuple(i.toString())

function _TupleToIndex(_Tuple i) returns int
    return i.num.toInt()

@Test function atest()
    let num = new Storage<int>
    num.t.assertEquals(0)

    num.set(2)
    num.t.assertEquals(2)

    let str = new Storage<string>
    str.set("3")
    str.t.assertEquals("3")

    let _class = new Storage<Class>
    _class.set(new Class(4))
    _class.t.num.assertEquals(4)

    let _Tuple = new Storage<_Tuple>
    _Tuple.set(_Tuple("5"))
    _Tuple.t.num.assertEquals("5")

    let __enum = new Storage<_enum>
    __enum.set( _enum.A )
    __enum.t castTo int.assertEquals( _enum.A castTo int )
    __enum.set( _enum.B )
    __enum.t castTo int.assertEquals( _enum.B castTo int )

Even in the standard package LinkedList:

public class LinkedList<T>
	private var dummy = new LLEntry<T>(null, null, null)
	...

class LLEntry<T>
	T elem
	LLEntry<T> prev
	LLEntry<T> next

	construct(T elem, LLEntry<T> prev, LLEntry<T> next)
		this.elem = elem -- set null
		this.prev = prev
		this.next = next

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants